package com.teng.stumanage;

import com.teng.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManage {
    private static ArrayList<Student> stuDataBase = new ArrayList<>();

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        while (true) {
            System.out.println("------------欢迎来到学生管理系统------------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查看学生");
            System.out.println("5.退出");
            System.out.print("请输入您的选择>>>");
            String choice = sc.nextLine();

            switch (choice) {
                case "1":
                    System.out.println("【添加学生】");
                    addStudent();
                    break;
                case "2":
                    System.out.println("【删除学生】");
                    delStudent();
                    break;
                case "3":
                    System.out.println("【修改学生】");
                    modStudent();
                    break;
                case "4":
                    System.out.println("【查看学生】");
                    queryStudent();
                    break;
                case "5":
                    System.out.println("感谢您的使用!");
                    System.exit(0);
                    break;
                default:
                    System.out.println("您的输入有误，请重新输入");
                    break;
            }
        }

    }

    private static Student parseOneStudent(String info) {

        String[] stuInfo = info.split(",");
        if (stuInfo.length != 4) {
            stuInfo = info.split("，");
            if (stuInfo.length != 4) {
                System.out.println("您的输入有误!");
                return null;
            }
        }

        String name = stuInfo[0];
        String sid = stuInfo[1];
        int age = Integer.parseInt(stuInfo[2]);
        String birthday = stuInfo[3];

        if (getIndex(sid) != -1) {
            System.out.println("学号已经存在！");
            return null;
        }

        return new Student(name, sid, age, birthday);
    }

    private static void addStudent() {
        Scanner sc = new Scanner(System.in);

        System.out.println("请按如下格式输入学生信息：");
        System.out.println("姓名，学号，年龄，生日");
        System.out.println("信息间以半角或全角逗号分隔");
        System.out.println("录入完成后按回车键确认，全部完成后输入0返回上一级菜单");

        while (true) {
            System.out.print(">>>");
            String info = sc.nextLine();
            if (info.equals("0")) {
                break;
            } else {
                Student stu = parseOneStudent(info);
                if (stu != null) {
                    stuDataBase.add(stu);
                }
            }
        }
    }

    private static void queryStudent() {
        System.out.println("姓名\t学号\t年龄\t生日");
        for (Student stu : stuDataBase) {
            String name = stu.getName();
            String sid = stu.getSid();
            int age = stu.getAge();
            String birthday = stu.getBirthday();
            System.out.println(name + "\t" + sid + "\t" + age + "\t" + birthday);
        }
    }

    private static int getIndex(String sid) {
        int index = -1;

        for (int i = 0; i < stuDataBase.size(); i++) {
            if (sid.equals(stuDataBase.get(i).getSid())) {
                index = i;
                break;
            }
        }

        return index;
    }

    private static void delStudent() {
        Scanner sc = new Scanner(System.in);

        System.out.print("请输入要删除的学生学号>>>");
        String sid = sc.nextLine();
        int index = getIndex(sid);
        if (index == -1) {
            System.out.println("查无学生信息！");
        } else {
            stuDataBase.remove(index);
            System.out.println("删除成功！");
        }
    }

    private static void modStudent() {
        Scanner sc = new Scanner(System.in);

        System.out.print("请输入要修改的学生学号>>>");
        String sid = sc.nextLine();
        int index = getIndex(sid);
        if (index == -1) {
            System.out.println("查无学生信息！");
        } else {
            System.out.println("请按如下格式输入学生信息：");
            System.out.println("姓名，学号，年龄，生日");
            System.out.println("信息间以半角或全角逗号分隔");
            System.out.println("录入完成后按回车键确认，全部完成后输入0返回上一级菜单");


            System.out.print(">>>");
            String info = sc.nextLine();
            Student stu = parseOneStudent(info);
            if (stu != null) {
                stuDataBase.set(index, stu);
                System.out.println("修改成功！");
            }
        }
    }
}
